import axios from "axios";
import storageUtil from "@/utils/storageUtil";
import router from "@/router";

let config = {
  baseURL:'',
  timeout:10000,
}
let instance = axios.create(config);

//请求拦截器
instance.interceptors.request.use(config=>{
  let uid = storageUtil.get("uid");
  let token = storageUtil.get("token");

  if(uid && token){
    //添加头信息
    config.headers.uid = uid;
    config.headers.token = token;
  }
  return config;
});

//响应拦截器
instance.interceptors.response.use(resp=>{
  // console.log(resp);
  if(resp.status == 200){
    let data = resp.data;
    if(data.code === 0){
      return data;
    }else if(data.code == 2) {
      //没有登录
      router.push("/login");
    }else{
      alert(data.msg);
    }
  }
  return resp;
})



export default {
  install(app){
    //vue3设置全局属性
    app.config.globalProperties.$axios = instance;
  }
}
